package com.cosma.xiaohashu.auth.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.cosma.framework.common.Utils.JsonUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author yusu_
 */
@Component
@Slf4j
public class AlinyunSmsHelper {
    @Resource
    private Client smsClient;

    public boolean sendMessage(String signName,String templateCode,String phone,String templateParam) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phone)
                .setTemplateParam(templateParam);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            log.info("==> 开始发送短信，phone:{},signName:{},templateCode:{},templateParam:{}",phone,signName,templateCode,templateParam);
            //发送短信
            SendSmsResponse response = smsClient.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info("==> 短信发送成功,response:{}", JsonUtils.toJsonString(response));
            return true;
        }catch (Exception error) {
            log.error("==> 发送短信错误: ",error);
        }
        return false;
    }
}
